﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using System.Globalization;
using DCTLib.Common;

public partial class DiagramUser : System.Web.UI.Page
{
    protected string genFunctionJs = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Session["User"].ToString()))
            {
                litBuildHtml.Text = RawHtml(Session["User"].ToString());
                genFunctionJs = "onload=\"CreateTree();\"";
            }
        }
    }

    private int widthCell = 130;
    private int heightCell = 53;

    protected bool isContraction
    {
        get
        {
            bool flag;
            try
            {
                flag = Convert.ToBoolean(Request.QueryString["contract"]);
            }
            catch
            {
                flag = false;
            }
            return flag;
        }
    }

    public static string StringConvertUserNum(int input)
    {
        NumberFormatInfo nfi = new NumberFormatInfo();
        nfi.NumberDecimalDigits = 0;
        nfi.NumberGroupSeparator = ",";
        try
        {
            return input.ToString("N", nfi);
        }
        catch
        {
            return input.ToString();
        }
    }

    public string StringConvertPoint(decimal input)
    {
        NumberFormatInfo nfi = new NumberFormatInfo();
        nfi.NumberDecimalDigits = 2;
        nfi.NumberGroupSeparator = ",";
        try
        {
            return input.ToString("N", nfi);
        }
        catch
        {
            return input.ToString();
        }
    }

    private string SumOfUser(string uName)
    {
        string sql = string.Format("select memberid from Web_Members where username='{0}'", uName);
        int userNum = 0;
        if (!string.IsNullOrEmpty(uName))
        {            
            sql = string.Format(@"DECLARE @levelCode varchar(1000)
                SET @levelCode = cast((SELECT MemberID FROM Web_Members WHERE username ='{0}') AS varchar)+'|'
                SELECT count(*) FROM Web_Members 
                WHERE UniqueCode like '%'+@levelCode+'%'", uName);
            userNum = int.Parse(SQLHelper.TExecuteScalar(sql).ToString());            
        }
        return StringConvertUserNum(userNum);
    }

    private string SumOfPoint(string uName)
    {
        decimal sumPoint = 0;
        string sql = string.Format(@"DECLARE @levelCode varchar(1000)
            SET @levelCode = cast((SELECT MemberID FROM Web_Members WHERE username ='{0}') AS varchar)
            SELECT coalesce(sum(pointNumber), 0) FROM
            (            
                SELECT coalesce(sum(pointNumber), 0) AS pointNumber
                FROM [Property] WHERE memberID IN
                (
	                SELECT MemberID FROM Web_Members 
	                WHERE UniqueCode like '%'+@levelCode+'|%'
                )
                UNION ALL
                SELECT coalesce(sum(pointNumber), 0) AS pointNumber
                FROM [Property] WHERE MemberID=@levelCode
            ) AS A", uName);
        sumPoint = decimal.Parse(SQLHelper.TExecuteScalar(sql).ToString());
        return StringConvertPoint(sumPoint);
    }

    private string PointOfUser(string uName)
    {
        decimal sumPoint = 0;
        string sql = string.Format(@"SELECT coalesce(sum(pointNumber), 0) AS pointNumber
            FROM [Property] WHERE MemberID=cast((SELECT MemberID FROM Web_Members WHERE username ='{0}') AS varchar)", uName);
        sumPoint = decimal.Parse(SQLHelper.TExecuteScalar(sql).ToString());
        return StringConvertPoint(sumPoint);
    }

    private string printFullText(string username)
    {
        string userTemp = string.Empty;
        if (username.Length > 11)
            userTemp = username.Substring(0, 11) + "...";
        else
            userTemp = username;
        if (isContraction)
            return string.Format("<a title=\"{0}\"><b>{1}</b></a>", username, userTemp);
        else
            return string.Format("<a title=\"{0}\"><b>{1}</b></a> (" + PointOfUser(username) + ")<br>&loz; Total: "
                + SumOfPoint(username) + "<br>&loz; " + SumOfUser(username) + " Users", username, userTemp);
    }

    protected string RawHtml(string userLogin)
    {
        if (isContraction) { widthCell = 100; heightCell = 20; }

        List<int> ListMemIdValid = new List<int>();
        string sql = string.Format(@"SELECT memberID FROM Web_Members
            WHERE username ='{0}'
            UNION ALL 
            SELECT memberID FROM Web_Members
            WHERE UniqueCode like '%'+cast((SELECT MemberID FROM Web_Members WHERE username ='{0}') AS varchar)+'|%'", userLogin);
        DataTable tbListMemID = SQLHelper.GetAll(sql);
        if (tbListMemID != null && tbListMemID.Rows.Count > 0)
        {
            for (int i = 0; i < tbListMemID.Rows.Count; i++)
            {
                ListMemIdValid.Add(int.Parse(tbListMemID.Rows[i][0].ToString()));
            }
        }


        StringBuilder HTMLCode = new StringBuilder();
        HTMLCode.Append("<script>");
        HTMLCode.Append("var myTree = null;");
        HTMLCode.Append(@"
            function CreateTree() {
			    myTree = new ECOTree('myTree','myTreeContainer');	
			    myTree.config.colorStyle = ECOTree.CS_LEVEL;
			    myTree.config.nodeFill = ECOTree.NF_FLAT;
			    myTree.config.useTarget = false;
			    myTree.config.selectMode = ECOTree.SL_NONE;
			    myTree.config.defaultNodeWidth = " + widthCell + @";
			    myTree.config.defaultNodeHeight = " + heightCell + @";
			    myTree.config.iSubtreeSeparation = 20;
			    myTree.config.iSiblingSeparation = 15;
			    myTree.config.iLevelSeparation = 30;");

        int maxLevel = 0;
        //get max level
        string sqlmaxlevel = "select max(LevelInfo) from Web_Members";
        string sqlMemberFilterByLevelInfo = "select * from Web_Members where LevelInfo={0} order by RegDate asc";
        maxLevel = int.Parse(SQLHelper.TExecuteScalar(sqlmaxlevel).ToString());
        

        //Level 0
        int level = 0;
        sql = string.Format(sqlMemberFilterByLevelInfo, level);
        DataTable tbListLevel = SQLHelper.GetAll(sql);
        if (tbListLevel != null && tbListLevel.Rows.Count > 0 && maxLevel > level)
        {
            for (int i = 0; i < tbListLevel.Rows.Count; i++)
            {
                int uIdLevel = int.Parse(tbListLevel.Rows[i]["MemberID"].ToString());
                string uName = tbListLevel.Rows[i]["Username"].ToString();
                if (userLogin.ToLower().Equals(uName.ToLower()))
                {                    
                    HTMLCode.Append(string.Format("myTree.add({0},-1,'{1}');", uIdLevel, printFullText(uName)));
                }

                //Level 1
                level = 1;
                sql = string.Format(@sqlMemberFilterByLevelInfo, level);
                DataTable tbListLevel1 = SQLHelper.GetAll(sql);
                if (tbListLevel1 != null && tbListLevel1.Rows.Count > 0 && maxLevel >= level)
                {
                    for (int i1 = 0; i1 < tbListLevel1.Rows.Count; i1++)
                    {
                        string ucLevel1 = tbListLevel1.Rows[i1]["UniqueCode"].ToString();
                        if (!string.IsNullOrEmpty(ucLevel1))
                        {
                            if (ucLevel1.Contains("|") && tbListLevel1.Rows[i1]["LevelInfo"].ToString() == "1")
                            {
                                int uIdTmp1 = int.Parse(ucLevel1.Substring(0, ucLevel1.IndexOf('|')));
                                if (uIdTmp1 == uIdLevel)
                                {
                                    int uIdLevel1 = int.Parse(tbListLevel1.Rows[i1]["MemberID"].ToString());
                                    string uNameLevel1 = tbListLevel1.Rows[i1]["Username"].ToString();
                                    if (userLogin.ToLower().Equals(uNameLevel1.ToLower()))
                                    {                                        
                                        HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel1, -1, printFullText(uNameLevel1)));
                                    }
                                    else
                                        if (ListMemIdValid.Contains(uIdLevel))
                                        HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel1, uIdLevel, printFullText(uNameLevel1)));

                                    //Level 2
                                    level = 2;
                                    sql = string.Format(sqlMemberFilterByLevelInfo, level);
                                    DataTable tbListLevel2 = SQLHelper.GetAll(sql);
                                    if (tbListLevel2 != null && tbListLevel2.Rows.Count > 0 && maxLevel >= level)
                                    {
                                        for (int i2 = 0; i2 < tbListLevel2.Rows.Count; i2++)
                                        {
                                            string ucLevel2 = tbListLevel2.Rows[i2]["UniqueCode"].ToString();
                                            if (!string.IsNullOrEmpty(ucLevel2))
                                            {
                                                if (ucLevel2.Contains("|") && tbListLevel2.Rows[i2]["LevelInfo"].ToString() == "2")
                                                {
                                                    int uIdTmp2 = int.Parse(ucLevel2.Substring(0, ucLevel2.IndexOf('|')));
                                                    if (uIdTmp2 == uIdLevel1)
                                                    {
                                                        int uIdLevel2 = int.Parse(tbListLevel2.Rows[i2]["MemberID"].ToString());
                                                        string uNameLevel2 = tbListLevel2.Rows[i2]["Username"].ToString();
                                                        if (userLogin.ToLower().Equals(uNameLevel2.ToLower()))
                                                        {                                                            
                                                            HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel2, -1, printFullText(uNameLevel2)));
                                                        }
                                                        else
                                                            if (ListMemIdValid.Contains(uIdLevel1))
                                                            HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel2, uIdLevel1, printFullText(uNameLevel2)));

                                                        //Level 3
                                                        level = 3;
                                                        sql = string.Format(sqlMemberFilterByLevelInfo, level);
                                                        DataTable tbListLevel3 = SQLHelper.GetAll(sql);
                                                        if (tbListLevel3 != null && tbListLevel3.Rows.Count > 0 && maxLevel >= level)
                                                        {
                                                            for (int i3 = 0; i3 < tbListLevel3.Rows.Count; i3++)
                                                            {
                                                                string ucLevel3 = tbListLevel3.Rows[i3]["UniqueCode"].ToString();
                                                                if (!string.IsNullOrEmpty(ucLevel3))
                                                                {
                                                                    if (ucLevel3.Contains("|") && tbListLevel3.Rows[i3]["LevelInfo"].ToString() == "3")
                                                                    {
                                                                        int uIdTmp3 = int.Parse(ucLevel3.Substring(0, ucLevel3.IndexOf('|')));
                                                                        if (uIdTmp3 == uIdLevel2)
                                                                        {
                                                                            int uIdLevel3 = int.Parse(tbListLevel3.Rows[i3]["MemberID"].ToString());
                                                                            string uNameLevel3 = tbListLevel3.Rows[i3]["Username"].ToString();
                                                                            if (userLogin.ToLower().Equals(uNameLevel3.ToLower()))
                                                                            {                                                                                
                                                                                HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel3, -1, printFullText(uNameLevel3)));
                                                                            }
                                                                            else
                                                                                if (ListMemIdValid.Contains(uIdLevel2))
                                                                                HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel3, uIdLevel2, printFullText(uNameLevel3)));

                                                                            //Level 4
                                                                            level = 4;
                                                                            sql = string.Format(sqlMemberFilterByLevelInfo, level);
                                                                            DataTable tbListLevel4 = SQLHelper.GetAll(sql);
                                                                            if (tbListLevel4 != null && tbListLevel4.Rows.Count > 0 && maxLevel >= level)
                                                                            {
                                                                                for (int i4 = 0; i4 < tbListLevel4.Rows.Count; i4++)
                                                                                {
                                                                                    string ucLevel4 = tbListLevel4.Rows[i4]["UniqueCode"].ToString();
                                                                                    if (!string.IsNullOrEmpty(ucLevel4))
                                                                                    {
                                                                                        if (ucLevel4.Contains("|") && tbListLevel4.Rows[i4]["LevelInfo"].ToString() == "4")
                                                                                        {
                                                                                            int uIdTmp4 = int.Parse(ucLevel4.Substring(0, ucLevel4.IndexOf('|')));
                                                                                            if (uIdTmp4 == uIdLevel3)
                                                                                            {
                                                                                                int uIdLevel4 = int.Parse(tbListLevel4.Rows[i4]["MemberID"].ToString());
                                                                                                string uNameLevel4 = tbListLevel4.Rows[i4]["Username"].ToString();
                                                                                                if (userLogin.ToLower().Equals(uNameLevel4.ToLower()))
                                                                                                {                                                                                                    
                                                                                                    HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel4, -1, printFullText(uNameLevel4)));
                                                                                                }
                                                                                                else
                                                                                                    if (ListMemIdValid.Contains(uIdLevel3))
                                                                                                    HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel4, uIdLevel3, printFullText(uNameLevel4)));

                                                                                                //Level 5
                                                                                                level = 5;
                                                                                                sql = string.Format(sqlMemberFilterByLevelInfo, level);
                                                                                                DataTable tbListLevel5 = SQLHelper.GetAll(sql);
                                                                                                if (tbListLevel5 != null && tbListLevel5.Rows.Count > 0 && maxLevel >= level)
                                                                                                {
                                                                                                    for (int i5 = 0; i5 < tbListLevel5.Rows.Count; i5++)
                                                                                                    {
                                                                                                        string ucLevel5 = tbListLevel5.Rows[i5]["UniqueCode"].ToString();
                                                                                                        if (!string.IsNullOrEmpty(ucLevel5))
                                                                                                        {
                                                                                                            if (ucLevel5.Contains("|") && tbListLevel5.Rows[i5]["LevelInfo"].ToString() == "5")
                                                                                                            {
                                                                                                                int uIdTmp5 = int.Parse(ucLevel5.Substring(0, ucLevel5.IndexOf('|')));
                                                                                                                if (uIdTmp5 == uIdLevel4)
                                                                                                                {
                                                                                                                    int uIdLevel5 = int.Parse(tbListLevel5.Rows[i5]["MemberID"].ToString());
                                                                                                                    string uNameLevel5 = tbListLevel5.Rows[i5]["Username"].ToString();
                                                                                                                    if (userLogin.ToLower().Equals(uNameLevel5.ToLower()))
                                                                                                                    {                                                                                                //                        
                                                                                                                        HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel5, -1, printFullText(uNameLevel5)));
                                                                                                                    }
                                                                                                                    else
                                                                                                                        if (ListMemIdValid.Contains(uIdLevel4))
                                                                                                                        HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel5, uIdLevel4, printFullText(uNameLevel5)));

                                                                                                                    //Level 6
                                                                                                                    level = 6;
                                                                                                                    sql = string.Format(sqlMemberFilterByLevelInfo, level);
                                                                                                                    DataTable tbListLevel6 = SQLHelper.GetAll(sql);
                                                                                                                    if (tbListLevel6 != null && tbListLevel6.Rows.Count > 0 && maxLevel >= level)
                                                                                                                    {
                                                                                                                        for (int i6 = 0; i6 < tbListLevel6.Rows.Count; i6++)
                                                                                                                        {
                                                                                                                            string ucLevel6 = tbListLevel6.Rows[i6]["UniqueCode"].ToString();
                                                                                                                            if (!string.IsNullOrEmpty(ucLevel6))
                                                                                                                            {
                                                                                                                                if (ucLevel6.Contains("|") && tbListLevel6.Rows[i6]["LevelInfo"].ToString() == "6")
                                                                                                                                {
                                                                                                                                    int uIdTmp6 = int.Parse(ucLevel6.Substring(0, ucLevel6.IndexOf('|')));
                                                                                                                                    if (uIdTmp6 == uIdLevel5)
                                                                                                                                    {
                                                                                                                                        int uIdLevel6 = int.Parse(tbListLevel6.Rows[i6]["MemberID"].ToString());
                                                                                                                                        string uNameLevel6 = tbListLevel6.Rows[i6]["Username"].ToString();
                                                                                                                                        if (userLogin.ToLower().Equals(uNameLevel6.ToLower()))
                                                                                                                                        {                                                                                                //                                            
                                                                                                                                            HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel6, -1, printFullText(uNameLevel6)));
                                                                                                                                        }
                                                                                                                                        else
                                                                                                                                            if (ListMemIdValid.Contains(uIdLevel5))
                                                                                                                                            HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel6, uIdLevel5, printFullText(uNameLevel6)));

                                                                                                                                        //Level 7
                                                                                                                                        level = 7;
                                                                                                                                        sql = string.Format(sqlMemberFilterByLevelInfo, level);
                                                                                                                                        DataTable tbListLevel7 = SQLHelper.GetAll(sql);
                                                                                                                                        if (tbListLevel7 != null && tbListLevel7.Rows.Count > 0 && maxLevel >= level)
                                                                                                                                        {
                                                                                                                                            for (int i7 = 0; i7 < tbListLevel7.Rows.Count; i7++)
                                                                                                                                            {
                                                                                                                                                string ucLevel7 = tbListLevel7.Rows[i7]["UniqueCode"].ToString();
                                                                                                                                                if (!string.IsNullOrEmpty(ucLevel7))
                                                                                                                                                {
                                                                                                                                                    if (ucLevel7.Contains("|") && tbListLevel7.Rows[i7]["LevelInfo"].ToString() == "7")
                                                                                                                                                    {
                                                                                                                                                        int uIdTmp7 = int.Parse(ucLevel7.Substring(0, ucLevel7.IndexOf('|')));
                                                                                                                                                        if (uIdTmp7 == uIdLevel6)
                                                                                                                                                        {
                                                                                                                                                            int uIdLevel7 = int.Parse(tbListLevel7.Rows[i7]["MemberID"].ToString());
                                                                                                                                                            string uNameLevel7 = tbListLevel7.Rows[i7]["Username"].ToString();
                                                                                                                                                            if (userLogin.ToLower().Equals(uNameLevel7.ToLower()))
                                                                                                                                                            {                                                                                                //                                                                
                                                                                                                                                                HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel7, -1, printFullText(uNameLevel7)));
                                                                                                                                                            }
                                                                                                                                                            else
                                                                                                                                                                if (ListMemIdValid.Contains(uIdLevel6))
                                                                                                                                                                HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel7, uIdLevel6, printFullText(uNameLevel7)));

                                                                                                                                                            //Level 8
                                                                                                                                                            level = 8;
                                                                                                                                                            sql = string.Format(sqlMemberFilterByLevelInfo, level);
                                                                                                                                                            DataTable tbListLevel8 = SQLHelper.GetAll(sql);
                                                                                                                                                            if (tbListLevel8 != null && tbListLevel8.Rows.Count > 0 && maxLevel >= level)
                                                                                                                                                            {
                                                                                                                                                                for (int i8 = 0; i8 < tbListLevel8.Rows.Count; i8++)
                                                                                                                                                                {
                                                                                                                                                                    string ucLevel8 = tbListLevel8.Rows[i8]["UniqueCode"].ToString();
                                                                                                                                                                    if (!string.IsNullOrEmpty(ucLevel8))
                                                                                                                                                                    {
                                                                                                                                                                        if (ucLevel8.Contains("|") && tbListLevel8.Rows[i8]["LevelInfo"].ToString() == "8")
                                                                                                                                                                        {
                                                                                                                                                                            int uIdTmp8 = int.Parse(ucLevel8.Substring(0, ucLevel8.IndexOf('|')));
                                                                                                                                                                            if (uIdTmp8 == uIdLevel7)
                                                                                                                                                                            {
                                                                                                                                                                                int uIdLevel8 = int.Parse(tbListLevel8.Rows[i8]["MemberID"].ToString());
                                                                                                                                                                                string uNameLevel8 = tbListLevel8.Rows[i8]["Username"].ToString();
                                                                                                                                                                                if (userLogin.ToLower().Equals(uNameLevel8.ToLower()))
                                                                                                                                                                                {                                                                                                //                                                                                    
                                                                                                                                                                                    HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel8, -1, printFullText(uNameLevel8)));
                                                                                                                                                                                }
                                                                                                                                                                                else
                                                                                                                                                                                    if (ListMemIdValid.Contains(uIdLevel7))
                                                                                                                                                                                    HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel8, uIdLevel7, printFullText(uNameLevel8)));

                                                                                                                                                                                //Level 9
                                                                                                                                                                                level = 9;
                                                                                                                                                                                sql = string.Format(sqlMemberFilterByLevelInfo, level);
                                                                                                                                                                                DataTable tbListLevel9 = SQLHelper.GetAll(sql);
                                                                                                                                                                                if (tbListLevel9 != null && tbListLevel9.Rows.Count > 0 && maxLevel >= level)
                                                                                                                                                                                {
                                                                                                                                                                                    for (int i9 = 0; i9 < tbListLevel9.Rows.Count; i9++)
                                                                                                                                                                                    {
                                                                                                                                                                                        string ucLevel9 = tbListLevel9.Rows[i9]["UniqueCode"].ToString();
                                                                                                                                                                                        if (!string.IsNullOrEmpty(ucLevel9))
                                                                                                                                                                                        {
                                                                                                                                                                                            if (ucLevel9.Contains("|") && tbListLevel9.Rows[i9]["LevelInfo"].ToString() == "9")
                                                                                                                                                                                            {
                                                                                                                                                                                                int uIdTmp9 = int.Parse(ucLevel9.Substring(0, ucLevel9.IndexOf('|')));
                                                                                                                                                                                                if (uIdTmp9 == uIdLevel8)
                                                                                                                                                                                                {
                                                                                                                                                                                                    int uIdLevel9 = int.Parse(tbListLevel9.Rows[i9]["MemberID"].ToString());
                                                                                                                                                                                                    string uNameLevel9 = tbListLevel9.Rows[i9]["Username"].ToString();
                                                                                                                                                                                                    if (userLogin.ToLower().Equals(uNameLevel9.ToLower()))
                                                                                                                                                                                                    {                                                                                                //                                                                                                        
                                                                                                                                                                                                        HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel9, -1, printFullText(uNameLevel9)));
                                                                                                                                                                                                    }
                                                                                                                                                                                                    else
                                                                                                                                                                                                        if (ListMemIdValid.Contains(uIdLevel8))
                                                                                                                                                                                                        HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel9, uIdLevel8, printFullText(uNameLevel9)));

                                                                                                                                                                                                    //Level 10
                                                                                                                                                                                                    level = 10;
                                                                                                                                                                                                    sql = string.Format(sqlMemberFilterByLevelInfo, level);
                                                                                                                                                                                                    DataTable tbListLevel10 = SQLHelper.GetAll(sql);
                                                                                                                                                                                                    if (tbListLevel10 != null && tbListLevel10.Rows.Count > 0 && maxLevel >= level)
                                                                                                                                                                                                    {
                                                                                                                                                                                                        for (int i10 = 0; i10 < tbListLevel10.Rows.Count; i10++)
                                                                                                                                                                                                        {
                                                                                                                                                                                                            string ucLevel10 = tbListLevel10.Rows[i10]["UniqueCode"].ToString();
                                                                                                                                                                                                            if (!string.IsNullOrEmpty(ucLevel10))
                                                                                                                                                                                                            {
                                                                                                                                                                                                                if (ucLevel10.Contains("|") && tbListLevel10.Rows[i10]["LevelInfo"].ToString() == "10")
                                                                                                                                                                                                                {
                                                                                                                                                                                                                    int uIdTmp10 = int.Parse(ucLevel10.Substring(0, ucLevel10.IndexOf('|')));
                                                                                                                                                                                                                    if (uIdTmp10 == uIdLevel9)
                                                                                                                                                                                                                    {
                                                                                                                                                                                                                        int uIdLevel10 = int.Parse(tbListLevel10.Rows[i10]["MemberID"].ToString());
                                                                                                                                                                                                                        string uNameLevel10 = tbListLevel10.Rows[i10]["Username"].ToString();
                                                                                                                                                                                                                        if (userLogin.ToLower().Equals(uNameLevel10.ToLower()))
                                                                                                                                                                                                                        {                                                                                                //                                                                                                                            
                                                                                                                                                                                                                            HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel10, -1, printFullText(uNameLevel10)));
                                                                                                                                                                                                                        }
                                                                                                                                                                                                                        else
                                                                                                                                                                                                                            if (ListMemIdValid.Contains(uIdLevel9))
                                                                                                                                                                                                                            HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel10, uIdLevel9, printFullText(uNameLevel10)));

                                                                                                                                                                                                                        //Level 11
                                                                                                                                                                                                                        level = 11;
                                                                                                                                                                                                                        sql = string.Format(sqlMemberFilterByLevelInfo, level);
                                                                                                                                                                                                                        DataTable tbListLevel11 = SQLHelper.GetAll(sql);
                                                                                                                                                                                                                        if (tbListLevel11 != null && tbListLevel11.Rows.Count > 0 && maxLevel >= level)
                                                                                                                                                                                                                        {
                                                                                                                                                                                                                            for (int i11 = 0; i11 < tbListLevel11.Rows.Count; i11++)
                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                string ucLevel11 = tbListLevel11.Rows[i11]["UniqueCode"].ToString();
                                                                                                                                                                                                                                if (!string.IsNullOrEmpty(ucLevel11))
                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                    if (ucLevel11.Contains("|") && tbListLevel11.Rows[i11]["LevelInfo"].ToString() == "11")
                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                        int uIdTmp11 = int.Parse(ucLevel11.Substring(0, ucLevel11.IndexOf('|')));
                                                                                                                                                                                                                                        if (uIdTmp11 == uIdLevel10)
                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                            int uIdLevel11 = int.Parse(tbListLevel11.Rows[i11]["MemberID"].ToString());
                                                                                                                                                                                                                                            string uNameLevel11 = tbListLevel11.Rows[i11]["Username"].ToString();
                                                                                                                                                                                                                                            if (userLogin.ToLower().Equals(uNameLevel11.ToLower()))
                                                                                                                                                                                                                                            {                                                                                                //                                                                                                                                                
                                                                                                                                                                                                                                                HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel11, -1, printFullText(uNameLevel11)));
                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                            else
                                                                                                                                                                                                                                                if (ListMemIdValid.Contains(uIdLevel10))
                                                                                                                                                                                                                                                HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel11, uIdLevel10, printFullText(uNameLevel11)));

                                                                                                                                                                                                                                            //Level 12
                                                                                                                                                                                                                                            level = 12;
                                                                                                                                                                                                                                            sql = string.Format(sqlMemberFilterByLevelInfo, level);
                                                                                                                                                                                                                                            DataTable tbListLevel12 = SQLHelper.GetAll(sql);
                                                                                                                                                                                                                                            if (tbListLevel12 != null && tbListLevel12.Rows.Count > 0 && maxLevel >= level)
                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                for (int i12 = 0; i12 < tbListLevel12.Rows.Count; i12++)
                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                    string ucLevel12 = tbListLevel12.Rows[i12]["UniqueCode"].ToString();
                                                                                                                                                                                                                                                    if (!string.IsNullOrEmpty(ucLevel12))
                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                        if (ucLevel12.Contains("|") && tbListLevel12.Rows[i12]["LevelInfo"].ToString() == "12")
                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                            int uIdTmp12 = int.Parse(ucLevel12.Substring(0, ucLevel12.IndexOf('|')));
                                                                                                                                                                                                                                                            if (uIdTmp12 == uIdLevel11)
                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                int uIdLevel12 = int.Parse(tbListLevel12.Rows[i12]["MemberID"].ToString());
                                                                                                                                                                                                                                                                string uNameLevel12 = tbListLevel12.Rows[i12]["Username"].ToString();
                                                                                                                                                                                                                                                                if (userLogin.ToLower().Equals(uNameLevel12.ToLower()))
                                                                                                                                                                                                                                                                {                                                                                                //                                                                                                                                                                    
                                                                                                                                                                                                                                                                    HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel12, -1, printFullText(uNameLevel12)));
                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                else
                                                                                                                                                                                                                                                                    if (ListMemIdValid.Contains(uIdLevel11))
                                                                                                                                                                                                                                                                    HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel12, uIdLevel11, printFullText(uNameLevel12)));

                                                                                                                                                                                                                                                                //Level 13
                                                                                                                                                                                                                                                                level = 13;
                                                                                                                                                                                                                                                                sql = string.Format(sqlMemberFilterByLevelInfo, level);
                                                                                                                                                                                                                                                                DataTable tbListLevel13 = SQLHelper.GetAll(sql);
                                                                                                                                                                                                                                                                if (tbListLevel13 != null && tbListLevel13.Rows.Count > 0 && maxLevel >= level)
                                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                                    for (int i13 = 0; i13 < tbListLevel13.Rows.Count; i13++)
                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                        string ucLevel13 = tbListLevel13.Rows[i13]["UniqueCode"].ToString();
                                                                                                                                                                                                                                                                        if (!string.IsNullOrEmpty(ucLevel13))
                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                            if (ucLevel13.Contains("|") && tbListLevel13.Rows[i13]["LevelInfo"].ToString() == "13")
                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                int uIdTmp13 = int.Parse(ucLevel13.Substring(0, ucLevel13.IndexOf('|')));
                                                                                                                                                                                                                                                                                if (uIdTmp13 == uIdLevel12)
                                                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                                                    int uIdLevel13 = int.Parse(tbListLevel13.Rows[i13]["MemberID"].ToString());
                                                                                                                                                                                                                                                                                    string uNameLevel13 = tbListLevel13.Rows[i13]["Username"].ToString();
                                                                                                                                                                                                                                                                                    if (userLogin.ToLower().Equals(uNameLevel13.ToLower()))
                                                                                                                                                                                                                                                                                    {                                                                                                //                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                        HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel13, -1, printFullText(uNameLevel13)));
                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                    else
                                                                                                                                                                                                                                                                                        if (ListMemIdValid.Contains(uIdLevel12))
                                                                                                                                                                                                                                                                                        HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel13, uIdLevel12, printFullText(uNameLevel13)));

                                                                                                                                                                                                                                                                                    //Level 14
                                                                                                                                                                                                                                                                                    level = 14;
                                                                                                                                                                                                                                                                                    sql = string.Format(sqlMemberFilterByLevelInfo, level);
                                                                                                                                                                                                                                                                                    DataTable tbListLevel14 = SQLHelper.GetAll(sql);
                                                                                                                                                                                                                                                                                    if (tbListLevel14 != null && tbListLevel14.Rows.Count > 0 && maxLevel >= level)
                                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                                        for (int i14 = 0; i14 < tbListLevel14.Rows.Count; i14++)
                                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                                            string ucLevel14 = tbListLevel14.Rows[i14]["UniqueCode"].ToString();
                                                                                                                                                                                                                                                                                            if (!string.IsNullOrEmpty(ucLevel14))
                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                if (ucLevel14.Contains("|") && tbListLevel14.Rows[i14]["LevelInfo"].ToString() == "14")
                                                                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                                                                    int uIdTmp14 = int.Parse(ucLevel14.Substring(0, ucLevel14.IndexOf('|')));
                                                                                                                                                                                                                                                                                                    if (uIdTmp14 == uIdLevel13)
                                                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                                                        int uIdLevel14 = int.Parse(tbListLevel14.Rows[i14]["MemberID"].ToString());
                                                                                                                                                                                                                                                                                                        string uNameLevel14 = tbListLevel14.Rows[i14]["Username"].ToString();
                                                                                                                                                                                                                                                                                                        if (userLogin.ToLower().Equals(uNameLevel14.ToLower()))
                                                                                                                                                                                                                                                                                                        {                                                                                                //                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                            HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel14, -1, printFullText(uNameLevel14)));
                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                        else
                                                                                                                                                                                                                                                                                                            if (ListMemIdValid.Contains(uIdLevel13))
                                                                                                                                                                                                                                                                                                            HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel14, uIdLevel13, printFullText(uNameLevel14)));

                                                                                                                                                                                                                                                                                                        //Level 15
                                                                                                                                                                                                                                                                                                        level = 15;
                                                                                                                                                                                                                                                                                                        sql = string.Format(sqlMemberFilterByLevelInfo, level);
                                                                                                                                                                                                                                                                                                        DataTable tbListLevel15 = SQLHelper.GetAll(sql);
                                                                                                                                                                                                                                                                                                        if (tbListLevel15 != null && tbListLevel15.Rows.Count > 0 && maxLevel >= level)
                                                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                                                            for (int i15 = 0; i15 < tbListLevel15.Rows.Count; i15++)
                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                string ucLevel15 = tbListLevel15.Rows[i15]["UniqueCode"].ToString();
                                                                                                                                                                                                                                                                                                                if (!string.IsNullOrEmpty(ucLevel15))
                                                                                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                                                                                    if (ucLevel15.Contains("|") && tbListLevel15.Rows[i15]["LevelInfo"].ToString() == "15")
                                                                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                                                                        int uIdTmp15 = int.Parse(ucLevel15.Substring(0, ucLevel15.IndexOf('|')));
                                                                                                                                                                                                                                                                                                                        if (uIdTmp15 == uIdLevel14)
                                                                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                                                                            int uIdLevel15 = int.Parse(tbListLevel15.Rows[i15]["MemberID"].ToString());
                                                                                                                                                                                                                                                                                                                            string uNameLevel15 = tbListLevel15.Rows[i15]["Username"].ToString();
                                                                                                                                                                                                                                                                                                                            if (userLogin.ToLower().Equals(uNameLevel15.ToLower()))
                                                                                                                                                                                                                                                                                                                            {                                                                                                //                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel15, -1, printFullText(uNameLevel15)));
                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                            else
                                                                                                                                                                                                                                                                                                                                if (ListMemIdValid.Contains(uIdLevel14))
                                                                                                                                                                                                                                                                                                                                HTMLCode.Append(string.Format("myTree.add({0},{1},'{2}');", uIdLevel15, uIdLevel14, printFullText(uNameLevel15)));
                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                        //end Level 15
                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                    //end Level 14
                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                //end Level 13
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                            //end Level 12
                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                }
                                                                                                                                                                                                                            }
                                                                                                                                                                                                                        }
                                                                                                                                                                                                                        //end Level 11
                                                                                                                                                                                                                    }
                                                                                                                                                                                                                }
                                                                                                                                                                                                            }
                                                                                                                                                                                                        }
                                                                                                                                                                                                    }
                                                                                                                                                                                                    //end Level 10
                                                                                                                                                                                                }
                                                                                                                                                                                            }
                                                                                                                                                                                        }
                                                                                                                                                                                    }
                                                                                                                                                                                }
                                                                                                                                                                                //end Level 9
                                                                                                                                                                            }
                                                                                                                                                                        }
                                                                                                                                                                    }
                                                                                                                                                                }
                                                                                                                                                            }
                                                                                                                                                            //end Level 8
                                                                                                                                                        }
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                        //end Level 7
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                    //end Level 6
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                                //end Level 5
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                            //end Level 4
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        //end Level 3
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    //end Level 2
                                }
                            }
                        }
                    }
                }
                //end Level 1
            }
        }
        //end Level 0

        HTMLCode.Append("myTree.UpdateTree();");
        HTMLCode.Append("}");
        HTMLCode.Append("</script>");

        return HTMLCode.ToString();
    }
}
